package cn.rkylin.oms.warehouse.stockin.service;

import cn.rkylin.core.service.ApolloService;
import cn.rkylin.oms.common.consts.StockInStatus;
import cn.rkylin.oms.common.consts.YesNoConst;
import cn.rkylin.oms.warehouse.stock.service.IStockService;
import cn.rkylin.oms.warehouse.stock.vo.StockVO;
import cn.rkylin.oms.warehouse.stockin.dao.StockInDAOImpl;
import cn.rkylin.oms.warehouse.stockin.dao.StockInGoodsDAOImpl;
import cn.rkylin.oms.warehouse.stockin.domain.StockIn;
import cn.rkylin.oms.warehouse.stockin.domain.StockInGoods;
import cn.rkylin.oms.warehouse.stockin.domain.StockinType;
import cn.rkylin.oms.warehouse.stockin.vo.StockInGoodsVO;
import cn.rkylin.oms.warehouse.stockin.vo.StockInVO;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 入库Service
 *
 * @author 齐涛
 * @version 1.0
 * @created 11-9-2017 10:51:30
 */
@Repository(value = "stockInService")
public class StockInServiceImpl extends ApolloService implements IStockInService {

    @Autowired
    private StockInDAOImpl stockInDAOImpl;

    @Autowired
    private StockInGoodsDAOImpl stockInGoodsDAOImpl;

    @Autowired
    private IStockService stockService;

    /**
     * 查询入库单列表
     *
     * @param page      页号
     * @param rows      页大小
     * @param stockInVO 入库单VO
     * @return 入库单列表
     * @throws Exception 异常
     */
    @SuppressWarnings("rawtypes")
    public PageInfo findByWhere(int page, int rows, StockInVO stockInVO) throws Exception {
        PageInfo<StockInVO> rdfundReasonTypeVOList = findPage(page, rows, "pageSelectStockIn", stockInVO);
        return rdfundReasonTypeVOList;
    }

    /**
     * 查询入库单商品列表
     *
     * @param stockInGoodsVO 入库单商品VO
     * @return 入库单信息
     * @throws Exception 异常
     */
    public List<StockInGoodsVO> selectStockInGoodsList(StockInGoodsVO stockInGoodsVO) throws Exception {
        return stockInGoodsDAOImpl.selectStockInGoodsList(stockInGoodsVO);
    }

    /**
     * 查询入库单信息
     *
     * @param stockInVO 入库单VO
     * @return 入库单信息
     * @throws Exception 异常
     */
    public StockInVO getStockInInfo(StockInVO stockInVO) throws Exception {
        StockInVO data = stockInDAOImpl.getStockInInfo(stockInVO);

        StockInGoodsVO stockInGoodsVO = new StockInGoodsVO();
        stockInGoodsVO.setStockinId(stockInVO.getStockinId());
        stockInGoodsVO.setOrderBy("goods_code, sku_code");
        List<StockInGoodsVO> goodsDataList = stockInGoodsDAOImpl.selectStockInGoodsList(stockInGoodsVO);
        data.setStockInGoodsVOList(goodsDataList);

        return data;
    }

    /**
     * 审核入库单状态
     *
     * @param stockinId 入库单Id
     * @throws Exception 异常
     */
    public void saveAuditStockIn(String stockinId) throws Exception {
        // 审核入库单
        StockIn stockIn = new StockIn();
        stockIn.setStockinId(stockinId);
        stockIn.setApprTime(new Date());
        stockIn.setStockinStatus("f");
        stockInDAOImpl.updateStockIn(stockIn);

        // 更新库存在途数
        this.updateOnwayQty(stockinId, 1);
    }

    /**
     * 删除入库单
     *
     * @param stockinId     入库单Id
     * @param stockInStatus 入库单状态
     * @throws Exception 异常
     */
    public void deleteStockIn(String stockinId, String stockInStatus) throws Exception {
        // 删除入库单
        StockIn stockIn = new StockIn();
        stockIn.setStockinId(stockinId);
        stockInDAOImpl.deleteStockIn(stockIn);

        // 待入库删除后更新库存在途数
        if (StockInStatus.WAIT_STOCKIN.equals(stockInStatus)) {
            this.updateOnwayQty(stockinId, -1);
        }
    }

    /**
     * 更新在途数
     *
     * @param stockinId 入库单Id
     * @param onwayQty  在途系数
     * @throws Exception 异常
     */
    private void updateOnwayQty(String stockinId, int onwayQtyCoefficient) throws Exception {
        // 得到入库单信息
        StockInVO stockInVO = new StockInVO();
        stockInVO.setStockinId(stockinId);
        StockInVO stockInInfo = stockInDAOImpl.getStockInInfoForUpdateOnwayQty(stockInVO);
        // 得到入库单SKU信息
        StockInGoodsVO stockInGoodsVO = new StockInGoodsVO();
        stockInGoodsVO.setStockinId(stockinId);
        List<StockInGoodsVO> stockInGoodsInfoList = stockInGoodsDAOImpl.selectStockInGoodsList(stockInGoodsVO);

        // 更新库存在途数
        for (StockInGoodsVO stockInGoodsInfo : stockInGoodsInfoList) {
            StockVO stockVO = new StockVO();
            stockVO.setEntId(stockInInfo.getEntId());
            stockVO.setSkuId(stockInGoodsInfo.getSkuId());
            stockVO.setStorId(stockInInfo.getStorId());
            stockVO.setSourceCode(stockInInfo.getStockinCode());
            stockVO.setTag("菜鸟入库单审核");
            stockVO.setOnwayQty(stockInGoodsInfo.getQty() * onwayQtyCoefficient);
            stockService.updateStock(stockVO);
        }
    }

    /**
     * 添加入库单
     *
     * @param stockInVO 入库单VO
     * @return 插入入库单条数
     * @throws Exception 异常
     */
    public int insertStockIn(StockInVO stockInVO) throws Exception {
        // 添加入库单
        StockIn stockIn = new StockIn();
        BeanUtils.copyProperties(stockInVO, stockIn);
        int insertCount = stockInDAOImpl.insertStockIn(stockIn);

        // 添加入库单商品
        List<StockInGoodsVO> list = stockInVO.getStockInGoodsVOList();
        for (int i = 0; i < list.size(); i++) {
            StockInGoodsVO stockInGoodsVO = list.get(i);
            StockInGoods stockInGoods = new StockInGoods();
            BeanUtils.copyProperties(stockInGoodsVO, stockInGoods);
            stockInGoodsDAOImpl.insertStockInGoods(stockInGoods);
        }

        return insertCount;
    }

    /**
     * 修改入库单(备注)
     *
     * @param stockInVO 入库单VO
     * @return 修改入库单备注条数
     * @throws Exception 异常
     */
    public int updateStockInRemark(StockInVO stockInVO) throws Exception {
        StockIn stockIn = new StockIn();
        BeanUtils.copyProperties(stockInVO, stockIn);
        int updateCount = stockInDAOImpl.updateStockIn(stockIn);

        return updateCount;
    }

    /**
     * 修改入库单
     *
     * @param stockInVO 入库单VO
     * @return 修改入库单条数
     * @throws Exception 异常
     */
    public int updateStockIn(StockInVO stockInVO) throws Exception {
        // 修改入库单
        StockIn stockIn = new StockIn();
        BeanUtils.copyProperties(stockInVO, stockIn);
        int updateCount = stockInDAOImpl.updateStockIn(stockIn);

        // 逻辑删除该入库单对应的商品
        StockInGoods stockInGoods = new StockInGoods();
        stockInGoods.setStockinId(stockInVO.getStockinId());
        stockInGoods.setDeleted(YesNoConst.YES);
        stockInGoodsDAOImpl.updateStockInGoods(stockInGoods);

        // 修改入库单商品
        List<StockInGoodsVO> list = stockInVO.getStockInGoodsVOList();
        for (int i = 0; i < list.size(); i++) {
            StockInGoodsVO stockInGoodsVO = list.get(i);
            StockInGoods goodsBean = new StockInGoods();
            BeanUtils.copyProperties(stockInGoodsVO, goodsBean);
            goodsBean.setDeleted(YesNoConst.NO);
            int result = stockInGoodsDAOImpl.updateStockInGoods(goodsBean);
            if (result == 0) {
                stockInGoodsDAOImpl.insertStockInGoods(goodsBean);
            }
        }

        return updateCount;
    }

    /**
     * 查询入库类型列表
     * @return
     * @throws Exception
     */
    public List<StockinType> selectStockinTypeList() throws Exception {
        return stockInDAOImpl.selectStockinTypeList();
    }

    @Override
    public int getUnGoingCount(Map<String, String> paramMap) throws Exception {
        return stockInDAOImpl.getUnGoingCount(paramMap);
    }

}
